import _ from 'lodash';

interface INode {
  id?: number,
  pid?: number
}

const helps = {
  treeToList: (treeData: any[], key?: string) => {
    let result: any[] = [];
    const deep = (dataList: any[]) => {
      for (let data of dataList) {
        key ? result.push(data[key]) : result.push(data);
        if (data.children) {
          deep(data.children);
        }
      }
    };
    deep(treeData);
    return result;
  },

  //递归查询后代
  findSons: (sourceList: INode[], node: INode) => {
    let result: any[] = [];
    const deepSons = (sourceList: INode[], node: INode) => {
      let sons = _.filter(sourceList, {pid: node.id});
      if (sons.length > 0) {
        result = result.concat(sons);
        for (let val of sons) {
          deepSons(sourceList, val);
        }
      }
    };
    deepSons(sourceList, node);
    return result;
  },
  //递归查询祖先
  findParents: (sourceList: INode[], node: INode, isCheckOperate: boolean = true, checkedList: any[]) => {
    let result: any[] = [];
    const deepParents = (sourceList: INode[], node: INode) => {
      //如果选中操作 或者 没有兄弟 再查询父亲
      console.log(helps.isHasBrother(sourceList, checkedList, node));
      if (isCheckOperate || !helps.isHasBrother(sourceList, checkedList, node)) {
        let parents = _.filter(sourceList, {id: node.pid});
        if (parents.length > 0) {
          result = result.concat(parents);
          for (let val of parents) {
            deepParents(sourceList, val);
          }
        }
      }
    };
    deepParents(sourceList, node);
    return result;
  },


  findHei: (sourceList: INode[], node: INode, isCheckOperate: boolean = true, prevCheckedList: any[] = []): any[] => {

    let result: any[] = [];
    console.log('findParents');
    const parents = helps.findParents(sourceList, node, isCheckOperate, prevCheckedList);
    result = result.concat(parents);
    result.push(node);
    const sons = helps.findSons(sourceList, node);
    result = result.concat(sons);

    const idMapResult = result.map((item) => item.id);

    if (isCheckOperate) {
      return _.uniq(prevCheckedList.concat(idMapResult));
    } else {
      _.remove(prevCheckedList, function (id) {
        return idMapResult.includes(id);
      });
      return _.uniq(prevCheckedList);
    }
  },

  findParentsAndSons: (sourceList: INode[], data: number, isCheckOperate: boolean = true, prevValue: any[] = []): any[] => {
    let result: any[] = [];
    let node = _.find(sourceList, function (o) {
      return o.id == data;
    });
    if (!node) return [];

    const parents = helps.findParents(sourceList, node, isCheckOperate, prevValue);
    result = result.concat(parents);
    result.push(node);
    const sons = helps.findSons(sourceList, node);
    result = result.concat(sons);

    return result;
  },
  //查询是否有兄弟节点   |   checkedList value 和 preValue都可以
  isHasBrother: (sourceList: INode[], checkedList: any[], node: INode): boolean => {
    const result = _.find(sourceList, function (o) {
      //如果有兄弟并且不是自己
      if (node.pid == o.pid && node.id != o.id) {
        //检查这个兄弟是不是在选中的情况
        return checkedList.includes(o.id);
      }
      return false;
    });
    return !!result;
  }
};
export default helps;
